/*
 * FuncionarioAdicionarAlterar.java
 *
 * Created on Jan 17, 2012, 2:45:13 PM
 */
package presentation.view.funcionario;

import domain.controller.FuncionarioController;
import javax.swing.JOptionPane;
import domain.pojo.Funcionario;
import presentation.view.JanelaInicial;
import presentation.view.MainFrame;
import presentation.view.paciente.PacienteListar;
import util.DefaultException;

/**
 *
 * @author Owner
 */
public class FuncionarioAdicionarAlterar extends javax.swing.JPanel {
    Funcionario funcionario = null;
    
    
    /** Creates new form FuncionarioAdicionarAlterar */
    public FuncionarioAdicionarAlterar() {
	initComponents();
    }

    public FuncionarioAdicionarAlterar(Funcionario funcionario, boolean admin){
	this.funcionario = funcionario;
	initComponents();
	if( funcionario == null){
	    jButton2.setText(" Adicionar ");
	} else {
	    jTextField1.setText(funcionario.getNome());
	    jTextField2.setText(funcionario.getLogin());
	    jPasswordField1.setText(funcionario.getSenha());
	    
	    if(funcionario.getTipo() == 0){
		jRadioButton1.setSelected(true);
		jRadioButton2.setEnabled(false);
	    }else{
		jRadioButton2.setSelected(true);
		jRadioButton1.setEnabled(false);
	    }
	    
	    jButton2.setText(" Atualizar ");
	}
	jRadioButton1.setEnabled(admin);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Adicionar novo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel1.setText("Nome");

        jLabel2.setText("Login");

        jLabel3.setText("Senha");

        jLabel4.setText("Administrador");

        jTextField1.setColumns(20);

        jTextField2.setColumns(20);

        jButton2.setText("Salvar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPasswordField1.setColumns(20);

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Sim");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Não");

        jButton3.setText("Limpar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setText("Voltar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(27, 27, 27)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jRadioButton2))
                            .addComponent(jPasswordField1)
                            .addComponent(jTextField2)
                            .addComponent(jTextField1)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(50, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(50, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(47, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
	jPasswordField1.setText("");
	jRadioButton1.setSelected(false);
	jRadioButton2.setSelected(false);
	jTextField1.setText("");
	jTextField2.setText("");
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
	MainFrame.getInstance().alterarPainel(MainFrame.getInstance().getPainelAnterior(), null);
	MainFrame.getInstance().retiraPilhaPainel();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
	if(validaBrancos()){
	    boolean radioButtonSelecionado = false;
	    if(jRadioButton1.isSelected()){
		radioButtonSelecionado = true;
	    }else if(jRadioButton2.isSelected()){
		radioButtonSelecionado = false;
	    }
	    if( funcionario == null){
		FuncionarioController mc = new FuncionarioController();
		Funcionario novoFuncionario = new Funcionario(0, jTextField2.getText(), jPasswordField1.getText() , jTextField1.getText(), radioButtonSelecionado);
		mc.incluir( novoFuncionario );
		JOptionPane.showMessageDialog(null, "Funcionario incluido com sucesso", "POO", JOptionPane.INFORMATION_MESSAGE);
		MainFrame.getInstance().alterarPainel(new PacienteListar(), JanelaInicial.getInstance());
	    } else {
		if( JOptionPane.showConfirmDialog(null, "Confirma Atualização do Funcionario?", "POO", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION ){
		    FuncionarioController mc = new FuncionarioController();
		    Funcionario funcionarioAtualizado = new Funcionario(funcionario.getId(), jTextField2.getText(), jPasswordField1.getText() , jTextField1.getText(), radioButtonSelecionado);
		    mc.atualizar(funcionarioAtualizado);
		    JOptionPane.showMessageDialog(null, "Funcionario atualizado com sucesso", "POO", JOptionPane.INFORMATION_MESSAGE);
		    MainFrame.getInstance().alterarPainel(new PacienteListar(), JanelaInicial.getInstance());
		}
	    }
	}
    }//GEN-LAST:event_jButton2ActionPerformed

    private boolean validaBrancos(){
	if(jTextField1.getText().equals("")){
	    JOptionPane.showMessageDialog(null, "Você deve inserir um nome", "POO", JOptionPane.ERROR_MESSAGE);
	    return false;
	} else if(jTextField2.getText().equals("")){
	    JOptionPane.showMessageDialog(null, "Você deve inserir um login", "POO", JOptionPane.ERROR_MESSAGE);
	    return false;
	} else if(jPasswordField1.getText().equals("")){
	    JOptionPane.showMessageDialog(null, "Você deve inserir uma senha", "POO", JOptionPane.ERROR_MESSAGE);
	    return false;
	} else if(!jRadioButton1.isSelected() && !jRadioButton2.isSelected()){
	    JOptionPane.showMessageDialog(null, "Você deve selecionar uma opção quanto admin", "POO", JOptionPane.ERROR_MESSAGE);
	    return false;
	}
	return true;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
